/**
 * cyw.com Inc.
 * Copyright (c) 2014-2015 All Rights Reserved.
 */
package com.rainbow.bam.service.account;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rainbow.bam.dao.HtAccountRepository;
import com.rainbow.bam.entity.HtAccount;
import com.rainbow.bam.entity.PaginationRequest;
import com.rainbow.bam.entity.PaginationResponse;
import com.rainbow.bam.enums.AccountEnum;
import com.rainbow.bam.exception.BusinessCheckException;
import com.rainbow.bam.util.Constant;
import com.rainbow.bam.util.DateUtil;
import com.rainbow.bam.util.Digests;
import com.rainbow.bam.util.Encodes;
import com.rainbow.bam.util.StringUtil;

/**
 * 账户接口服务实现类
 * 
 * @author HarrisonHan
 * @version $Id: HtAccountServiceImpl.java, v 0.1 2015年10月26日 上午10:16:02 HarrisonHan Exp $
 */
@Service
public class HtAccountServiceImpl implements HtAccountService {

    /**
     * 账户操作DAO
     */
    @Autowired
    private HtAccountRepository htAccountRepository;
    
    /** 
     * @see com.rainbow.bam.service.account.HtAccountService#findAccountById(java.lang.Long)
     */
    @Override
    public HtAccount findAccountById(Long id) {
        return htAccountRepository.findOne(id);
    }

    /** 
     * @see com.rainbow.bam.service.account.HtAccountService#findAccountByKey(java.lang.String)
     */
    @Override
    public HtAccount findAccountByKey(String key) {
        return htAccountRepository.findByAccountKey(key);
    }
    
    /** 
     * @see com.rainbow.bam.service.account.HtAccountService#findAccountsByPagination(com.huatek.shop.entity.PaginationRequest)
     */
    @Override
    public PaginationResponse<HtAccount> findAccountsByPagination(PaginationRequest paginationRequest) {
        return htAccountRepository.findResultsByPagination(paginationRequest);
    }

    /**
     * 
     * @see com.rainbow.bam.service.account.HtAccountService#addAccount(com.huatek.shop.entity.HtAccount)
     */
    @Override
    public void addAccount(HtAccount htAccount) throws BusinessCheckException {
        this.accountValidation(htAccount);//账户信息有效性检查
        htAccount.setAccountName(htAccount.getAccountName().toLowerCase());
        htAccount.setAccountKey(this.createAccountKey());
        htAccount.setAccountStatus(AccountEnum.ACCOUNT_VALID);
        htAccount.setIsActive(AccountEnum.ACCOUNT_NO_ACTIVE);
        htAccount.setCreateDate(new Date());
        htAccount.setModifyDate(new Date());
        this.entryptPassword(htAccount);
        htAccountRepository.save(htAccount);
    }

    /**
     * 生成账户编码
     * @return 账户编码
     */
    private String createAccountKey() {
        String year = DateUtil.formatMonth(new Date());//年月
        String time = Long.toString(new Date().getTime());//时间戳
        int random = (int) (Math.random() * (9999 - 1000 + 1)) + 1000;//4位随机
        return year + time + random;
    }

    /**
     * 验证账户信息有效性检查
     * 
     * @param htAccount 账户信息实体
     * @throws BusinessCheckException 业务检查异常
     */
    private void accountValidation(HtAccount htAccount) throws BusinessCheckException {
        if (htAccount == null) {
            throw new BusinessCheckException("请填写账户信息");
        }
        if (StringUtil.isEmpty(htAccount.getAccountName())) {
            throw new BusinessCheckException("请填写账户名称");
        }
        if (StringUtil.isEmpty(htAccount.getPassword())) {
            throw new BusinessCheckException("请填写密码");
        }
        if (htAccountRepository.findByAccountName(htAccount.getAccountName()) != null) {
            throw new BusinessCheckException(htAccount.getAccountName() + "已经存在");
        }
    }

    /** 
     * @see com.rainbow.bam.service.account.HtAccountService#deleteAccount(java.lang.String)
     */
    @Override
    public void deleteAccount(String accountKey) throws BusinessCheckException {
        if (StringUtil.isBlank(accountKey)) {
            throw new BusinessCheckException("账户编码不能为空");
        }
        HtAccount htaccount = htAccountRepository.findByAccountKey(accountKey);
        if(htaccount  == null){
            throw new BusinessCheckException("账户不存在");
        }
        htAccountRepository.delete(htaccount);
    }
    /**
     * 
     * @see com.rainbow.bam.service.account.HtAccountService#findByAccountName(java.lang.String)
     */
    public HtAccount findByAccountName(String accountName){
      return   this.htAccountRepository.findByAccountName(accountName);
    }
    
    /**
     * 设定安全的密码，生成随机的salt并经过1024次 sha-1 hash
     */
    private void entryptPassword(HtAccount user) {
        byte[] salt = Digests.generateSalt(Constant.SaltConstant.SALT_SIZE);
        user.setSalt(Encodes.encodeHex(salt));

        byte[] hashPassword = Digests.sha1(user.getPlainPassword().getBytes(), salt, Constant.SaltConstant.HASH_INTERATIONS);
        user.setPassword(Encodes.encodeHex(hashPassword));;
    }

}
